package youcaihuaoa.weetop.com.vm.login

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.StringUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.util.ToastUtils
import com.robot.base.utils.JobUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.http.BaseResponse
import youcaihuaoa.weetop.com.GlApp
import youcaihuaoa.weetop.com.R

class ForgetPasswordViewModel(application: Application) : GLBaseViewModel(application) {
    val promptSelect = ObservableField(false)
    val phone = ObservableField<String>()
    val code = ObservableField<String>()
    val password = ObservableField<String>()
    val newPassword = ObservableField<String>()
    val prompt = ObservableField<String>("获取验证码")
    val promptEnable = ObservableField(true)

    val promptClick = BindingCommand<Any>(BindingAction {
        promptSelect.set(!promptSelect.get()!!)
    })
    val sendCode = BindingCommand<Any>(BindingAction {
        sendSmsVerifyCode()
    })

    val confirmCommand = BindingCommand<Any>(BindingAction {
        if (checkPrompt())
            return@BindingAction
        if (checkPassword())
            return@BindingAction
        confirm()
//        toLoginType.postValue(SHARE_MEDIA.QQ)
    })

    private fun checkPrompt(): Boolean {
        if (promptSelect.get() == false) {
            ToastUtils.showShort(StringUtils.getString(R.string.app_error_4))
        }
        return promptSelect.get() == false
    }

    fun sendSmsVerifyCode() {
        if (TextUtils.isEmpty(phone.get())) {
            ToastUtils.showShort("手机号不能为空")
            return
        }
        if ((phone.get()?.length ?: 0) < 11) {
            ToastUtils.showShort("请输入11为手机号")
            return
        }

        val map = mapOf(
            "phone" to phone.get()
        )
        showLoadingDialog()
        UserServiceFactory.sendSmsVerifyCode(map)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    dismissLoadingDialog()

                    setViewData()
                    promptEnable.set(false)
                    ToastUtils.showShort(t.msg)
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
//                    ToastUtils.showShort(e.message)
                }
            })
    }

    private fun checkPassword(): Boolean {
        if (TextUtils.isEmpty(phone.get())) {
            ToastUtils.showShort(StringUtils.getString(R.string.app_error_1))
            return true
        }
        if ((phone.get()?.length ?: 0) < 11) {
            ToastUtils.showShort(StringUtils.getString(R.string.app_error_2))
            return true
        }

        if (TextUtils.isEmpty(code.get())) {
            ToastUtils.showShort(StringUtils.getString(R.string.app_error_9))
            return true
        }

        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort(StringUtils.getString(R.string.app_error_6))
            return true
        }
        if (TextUtils.isEmpty(newPassword.get())) {
            ToastUtils.showShort(StringUtils.getString(R.string.app_error_7))
            return true
        }
        if (!TextUtils.equals(password.get(), newPassword.get())) {
            ToastUtils.showShort(StringUtils.getString(R.string.app_error_5))
            return true
        }
        return false
    }
    private fun confirm() {

        showLoadingDialog()
        UserServiceFactory.findPsd(
            mapOf(
                "code" to code.get(),
                "new_psd_two" to newPassword.get(),
                "phone" to phone.get(),
                "new_psd" to password.get()
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                dismissLoadingDialog()
                finish()
            }
        })
    }

    private fun setViewData() {
        JobUtil.countDownCoroutines(
            60, {
                prompt.set("(${it})")//${getString(R.string.app_en_tips_33)}
            },
            {
                prompt.set("获取验证码")
                promptEnable.set(true)
            }
        ).start()
    }
}